/*
 * Copyright (c) 2010, artur
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * kiddie-os
 * Created on: 14.04.2010
 *     Author: artur
 *
 */

#ifndef OBJECT_H_
#define OBJECT_H_

#ifdef __KERNEL__
#include <sys/types.h>
#else
typedef unsigned int size_t;
typedef unsigned int u32;
typedef unsigned int uint32_t;
typedef unsigned short u16;
typedef unsigned short uint16_t;
typedef unsigned char u8;
typedef unsigned char uint8_t;
#endif

//#include <util/hash_map.h>
//#include <util/string.h>

/**
 * @class Object
 *
 * Attributes 'attributes' of an Object are numerical entries into
 * database of attributes called 'Alphabet' of a system.
 */
class Object
{
	private:
//		util::hash_map<util::string> attributes;

		unsigned long long ID; // Object's unique ID

		// left and right nodes
		Object *left;
		Object *right;

	public:
		Object();
		~Object();
		// union
		Object operator &(const Object& b) const;
		Object &operator &=(const Object& b);
		// sum
		Object operator |(const Object& b) const;
		Object &operator |=(const Object& b);
		//
		Object operator ^(const Object& b) const;
		Object &operator ^=(const Object& b);

		// add remove childs
		Object &operator +=(Object& b);
		Object &operator -=(Object& b);

		// find objects by 'pattern' regexp
		Object *locate (char *pattern);

		// find objects like 'example' object
		/**
		 * Returns Object which describes the response
		 * not an object requested. Such response may
		 * contain list of objects satisfying request
		 * with weights of satisfaction and other
		 * usefull information.
		 */
		Object *locate (Object *example);

};

/**
 *
 */
class IReader
{
	public:
		virtual uint32_t read32(size_t off) {}
		virtual uint32_t read16(size_t off) {}
		virtual uint32_t read8(size_t off) {}
		// TODO following funcs must not be virtual also with the 'cur' field!
		virtual uint32_t read32() {}
		virtual uint32_t read16() {}
		virtual uint32_t read8() {}
		virtual void jmp(int) {}
};

/**
 *
 */
class Reader : public IReader
{
	public:
		uint32_t cur;	// cursor

	public:
		virtual uint32_t read32blk(size_t off) = 0;
		uint32_t read32(size_t off);
		uint32_t read16(size_t off);
		uint32_t read8(size_t off);
		uint32_t read32();
		uint32_t read16();
		uint32_t read8();
		void jmp(int);
};

// Big endian reader class
// This is used in JVM
class BEReader : public IReader
{
	public:
		uint32_t cur;	// cursor

		IReader *src;
		BEReader(IReader *r);
		~BEReader();
		uint32_t read32blk(size_t off) {return 0;}
		uint32_t read32(size_t off);
		uint32_t read16(size_t off);
		uint32_t read8(size_t off);
		uint32_t read32();
		uint32_t read16();
		uint32_t read8();
		void jmp(int);
};

#endif /* OBJECT_H_ */
